/*******************************************************************************
 * 版权：北京润尼尔网络科技有限公司，保留所有版权<br>
 * 版本：v1.0.0<br>
 * 文件：RootEditPart.js<br>
 * 日期：2016年9月20日<br>
 * 作者:王志远<br>
 * 功能：跟编辑件<br>
 *
 * 修改记录：<br>
 * 日期 描述 更新功能<br>
 ******************************************************************************/

window.makeNamespace(window, "Rainier.UI.V2");

/**
 * 类型名称：Rainier.UI.V2.RootEditPart<br>
 * 类型功能：组合组件编辑件<br>
 */
Rainier.UI.V2.RootEditPart = Rainier.UI.V2.CompositeEditPart.extend({
  NAME: "Rainier.UI.V2.RootEditPart",

  /**
   * 函数名称：Rainier.UI.V2.RootEditPart.init<br>
   * 函数功能：构造函数<br>
   * 异常信息：无<br>
   */
  init: function () {
    this._super();
    this.helpLineEnabled = true;
    this.editPolicySnapToGeometry = new draw2d.policy.canvas.SnapToGeometryEditPolicy();
    this.editPolicySnapToCenter = new draw2d.policy.canvas.SnapToCenterEditPolicy();
    this.editPolicySnapToInBetween = new draw2d.policy.canvas.SnapToInBetweenEditPolicy();
    this.editPolicySnapToGrid = new draw2d.policy.canvas.SnapToGridEditPolicy(9);
    this.editPolicyShowDot = new draw2d.policy.canvas.ShowDotEditPolicy(9);
    this.editPolicyConnCreate = new Rainier.UI.V2.OrthogonalConnectionCreatePolicy();

    this.installEditPolicy(new draw2d.policy.canvas.CoronaDecorationPolicy());
    this.installEditPolicy(new Rainier.UI.V2.KeyboardEditPolicy());
    this.installEditPolicy(this.editPolicyConnCreate);
    this.installEditPolicy(this.editPolicySnapToGeometry);
    this.installEditPolicy(this.editPolicySnapToCenter);
    this.installEditPolicy(this.editPolicySnapToInBetween);
    this.installEditPolicy(this.editPolicySnapToGrid);

    this.modelEventListenerList = new draw2d.util.ArrayList();

    this.createInfo = {};
    this.isCreateState = false;
    this.createInfo.modelClass = null;
    this.createInfo.modelProperty = null;
  },

  /**
   * 函数名称：Rainier.UI.V2.RootEditPart.addModelEventListener<br>
   * 函数功能：添加模型事件侦听器<br>
   * 参数输入：eventListener:事件侦听器<br>
   * 函数返回：无<br>
   * 异常信息：无<br>
   */
  addModelEventListener: function (sListenerId, eventListener) {
    var listener = {};
    listener.id = sListenerId;
    listener.listener = eventListener;
    this.modelEventListenerList.add(listener);
    Rainier.Basic.CompositeObject.recursiveEach(this, function (part) {
      var model = part.getModel();
      model.addPropertyChangeListener(sListenerId, eventListener);
    });
  },

  /**
   * 函数名称：Rainier.UI.V2.RootEditPart.onFigureContextMenu<br>
   * 函数功能：当图元右键菜单时触发<br>
   * 参数输入：emitter<br>
   * 参数输入：figure<br>
   * 函数返回：无<br>
   * 异常信息：无<br>
   */
  onFigureContextMenu: function (emitter, figure) {
    return;
    //创建状态，鼠标右键退出创建状态
    if (this.getCreateState()) {
      this.exitCreateState();
      return;
    }
    //创建连线状态，鼠标右键退出创建连线状态
    if (this.editPolicyConnCreate.runState.current !== "none") {
      this.editPolicyConnCreate.reset();
      return;
    }

    var canvasEditpart = this;
    if (emitter instanceof Rainier.UI.V2.Canvas) {
      //_figure不存在，说明点击是帆布对象，直接显示对应的菜单。
      var rootCanvas = this.context.rootFigure;
      if (!figure.figure && !rootCanvas.getPrimarySelection()) {
        var canvasMenu = new Rainier.UI.V2.CanvasMenu(emitter, figure);
        canvasMenu.show();
      }
      if (rootCanvas.getPrimarySelection()) {
        var currentFigure = rootCanvas.getPrimarySelection();
        if (!figure.figure && currentFigure) {
          var figureEditPart = this.context.findEditPartByFigure(currentFigure);
          figure.figure = currentFigure;
          figureEditPart.onFigureContextMenu(emitter, figure);
        }
      }
    }
  },

  /**
   * 函数名称：Rainier.UI.V2.RootEditPart.removeModelEventListener<br>
   * 函数功能：删除模型事件侦听器<br>
   * 参数输入：eventListener:事件侦听器<br>
   * 函数返回：无<br>
   * 异常信息：无<br>
   */
  removeModelEventListenerById: function (sListenerId) {
    var listener = null;
    this.modelEventListenerList.each(function (i, lis) {
      if (lis.id === sListenerId) {
        listener = lis;
      }
    });
    this.modelEventListenerList.remove(listener);

    this.modelEventListenerList.remove(eventListener);
    Rainier.Basic.CompositeObject.recursiveEach(this, function (part) {
      var model = part.getModel();
      model.removePropertyChangeListener(sListenerId);
    });
  },

  /**
   * 函数名称：Rainier.UI.V2.RootEditPart.getModelEventListeners<br>
   * 函数功能：获取事件侦听器列表<br>
   * 函数返回：无<br>
   * 异常信息：无<br>
   */
  getModelEventListeners: function () {
    return this.modelEventListenerList;
  },

  /**
   * 函数名称：Rainier.UI.V2.RootEditPart.blankBakcGround<br>
   * 函数功能：使用空白背景<br>
   * 函数返回：无<br>
   * 异常信息：无<br>
   */
  blankBackground: function () {
    this.figure.uninstallEditPolicy(this.editPolicyShowDot);
    this.figure.uninstallEditPolicy(this.editPolicySnapToGrid);
  },

  /**
   * 函数名称：Rainier.UI.V2.RootEditPart.gridBackground<br>
   * 函数功能：使用网格背景<br>
   * 函数返回：无<br>
   * 异常信息：无<br>
   */
  gridBackground: function () {
    this.figure.uninstallEditPolicy(this.editPolicyShowDot);
    this.figure.installEditPolicy(this.editPolicySnapToGrid);
  },

  /**
   * 函数名称：Rainier.UI.V2.RootEditPart.dotBackground<br>
   * 函数功能：使用点格背景<br>
   * 函数返回：无<br>
   * 异常信息：无<br>
   */
  dotBackground: function () {
    this.figure.uninstallEditPolicy(this.editPolicySnapToGrid);
    this.figure.installEditPolicy(this.editPolicyShowDot);
  },

  /**
   * 函数名称：Rainier.UI.V2.RootEditPart.setDefaultRouterClassName<br>
   * 函数功能：通过路由器类名称，设置连接路由<br>
   * 参数输入：defaultRouterClassName:String<br>
   * 函数返回：无<br>
   * 异常信息：无<br>
   */
  setDefaultRouterClassName: function (defaultRouterClassName) {
    this.defaultRouterClassName = defaultRouterClassName;
    this.defaultRouter = eval("new " + this.defaultRouterClassName + "()");
    var router = this.defaultRouter;
    this.figure.getLines().each(function (i, line) {
      line.setRouter(router);
    });
  },

  /**
   * 函数名称：Rainier.UI.V2.RootEditPart.enterCreateState<br>
   * 函数功能：获取创建模型信息<br>
   * 函数返回：
   * {
   * 	    modelClass : 'Rainier.UI.V2.XXX_MODEL';
   *   	modelProperty : {
   *   		uiProperty 		: {},
   *   		modelProperty 	: {},
   *   		userProperty  	: {}
   *   	};
   * }<br>
   * 异常信息：<br>
   */
  getCreateInfo: function () {
    return this.createInfo;
  },

  /**
   * 函数名称：Rainier.UI.V2.RootEditPart.enterCreateState<br>
   * 函数功能：进入创建器件模式<br>
   * 参数输入：sModelClassName:模型名称<br>
   * 参数输入：property:属性名称<br>
   * 参数输入：json：模型Json描述符<br>
   * 函数返回：无<br>
   * 异常信息：无<br>
   */
  enterCreateState: function (sModelClassName, property, json) {
    this.isCreateState = true;
    this.createInfo.modelClass = sModelClassName;
    this.createInfo.modelProperty = property;
    this.createInfo.json = json;
    this.figure.html.css("cursor", "crosshair");
  },

  /**
   * 函数名称：Rainier.UI.V2.RootEditPart.exitCreateState<br>
   * 函数功能：离开创建模式<br>
   * 函数返回：无<br>
   * 异常信息：无<br>
   */
  exitCreateState: function () {
    this.isCreateState = false;
    this.createInfo.modelClass = null;
    this.createInfo.modelProperty = null;

    this.figure.html.css({ cursor: "default" });
  },

  /**
   * 函数名称：Rainier.UI.V2.RootEditPart.getCreateState<br>
   * 函数功能：是否处于创建模式<br>
   * 函数返回：无<br>
   * 异常信息：无<br>
   */
  getCreateState: function () {
    return this.isCreateState;
  },

  /**
   * 函数名称：Rainier.UI.V2.RootEditPart.getDefaultRouter<br>
   * 函数功能：获取默认连续路由器<br>
   * 函数返回：draw2d.connection.ConnectionRouter<br>
   * 异常信息：<br>
   */
  getDefaultRouter: function () {
    return this.defaultRouter;
  },

  /**
   * 函数名称：Rainier.UI.V2.RootEditPart.setFigure<br>
   * 函数功能：设置帆布<br>
   * 参数输入：canvas:Rainier.UI.V2.Canvas<br>
   * 函数返回：无<br>
   * 异常信息：无<br>
   */
  setFigure: function (canvas) {
    this.figure = canvas;
    this._super(canvas);
  },
});
